//=============================================================================
// BonusValue.as
// 
// 
// AUTHOR:            Jay Liang
// 
//                  Copyright (c) 2011 Break Media
//                              All Rights Reserved.
//                          
//=============================================================================
package values.common
{
import com.adobe.serialization.json.JSON;

import flash.utils.Dictionary;

import mx.utils.StringUtil;

import values.item.IItemValue;
import values.item.INumbericItemValue;
import values.item.ItemTemplateValueList;
import values.item.ItemValue;
import values.level.AvatarLevelValue;


public class BonusValue
{
	private static const XML_PATTERN:String = "<item index=\"{0}\" key=\"{1}\" num=\"{2}\" possibility=\"{3}\" level_required=\"{4}\" level_disable=\"{5}\" params='{6}' />";
	
	public static var DYNAMIC_PECT_MAP:Dictionary = new Dictionary(true);  /* Number */
	
	private static var level:AvatarLevelValue;
	
	private static function get currentPlayerLevel():int
	{
		return level ? level.level : 1;
	}
	
	public static function initialize(level:AvatarLevelValue):void
	{
		BonusValue.level = level;
	}
	
	public var params:Object = {};
    
    public function addItemGroupObject(key:String, num:Number, possibility:Number = 100, levelRequired:Number = 0, levelDisable:Number = -1, params:* = ""):void
    {
		var index:int = 1;
		if (_xmlData)
		{
			index = _xmlData.length() + 1;
		}
		
		var pstr:String;
		if (params != null && params != "")
		{
			try
			{
				pstr = JSON.encode(params);
			}
			catch (error:Error)
			{
				pstr = params;
			}
		}
		else
		{
			pstr = "";
		}
		
		var str:String = StringUtil.substitute(XML_PATTERN, index, key, num, possibility, levelRequired, levelDisable, pstr);
		if (!xmlData)
		{
			xmlData = new XMLList(str);
		}
		else
		{
			xmlData = new XMLList(xmlData.toXMLString() + "\n" + str);
		}
    }
	
	private var _xmlData:XMLList;
	
	public function get xmlData():XMLList
	{
		return _xmlData;
	}
	
	[Data(type="field", mapping="item")]
	public function set xmlData(value:XMLList):void
	{
		_xmlData = value;
	}
	
	public function get bonus():Vector.<IItemValue> /* IItemValue */
	{
		var temp:Vector.<IItemValue> = new Vector.<IItemValue>();
		var vo:IItemValue;
		
		if (xmlData == null || xmlData.toXMLString() == "") return temp;
		
		if (xmlData.length() > 0)
		{
			for each (var obj:* in xmlData)
			{
				vo = getSingleItemValue(obj);
				if (vo)  temp.push(vo);
			}
		}
		else if (xmlData.toXMLString() != "")
		{
			vo = getSingleItemValue(obj);
			if (vo) temp.push(vo);
		}
		
		return temp;
	}
	
	public function clearAll():void
	{
		_xmlData = null;
	}
	
	public function clone():BonusValue
	{
		var vo:BonusValue = new BonusValue();
		vo.xmlData = xmlData ? xmlData.copy() : null;
		return vo;
	}
	
	public static function getSingleItemValue(obj:*):IItemValue
	{
		if (int(obj.@level_required) > currentPlayerLevel) return null;
		
		if (int(obj.@level_disable) > 0 && currentPlayerLevel >= int(obj.@level_disable))  return null;
		
		var rand:Number = 100 * Math.random();
		
		var key:String = obj.@key;
		
		var pect:Number = String(obj.@possibility) == "" ? 100 : Number(obj.@possibility);
		
		if (DYNAMIC_PECT_MAP[key])
		{
			pect = DYNAMIC_PECT_MAP[key];
		}
		
		if (rand <= pect && pect != 0)
		{
			var params:* = obj.@params;
			if (params != null && params != "")
			{
				try
				{
					params = JSON.decode(params);
				}
				catch (error:Error)
				{
					//do nothing
				}
			}
			else
			{
				params = null;
			}
			var vo:IItemValue = ItemValue.create(ItemTemplateValueList.getByKey(key), null, params);
			if (vo is INumbericItemValue)
			{
				INumbericItemValue(vo).num = Number(obj.@num);
			}
			return vo;
		}
		return null;
	}
}
}



